<template>
    <div class="cate-container">
         <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
       <el-row><el-button type="primary" @click="showAddCateDialog">添加分类</el-button></el-row>
        <!-- 树形表格 -->
        <tree-table class="tree-table" :data="cateList" :columns="columns" :show-index="true" 
         :expand-type="false" :selection-type="false" :show-row-hover="false" border>
         <!-- 树形表格插槽 是否有效 -->
         <tempalte slot="isok" slot-scope="scope">
            <i class="el-icon-success"  else></i>
             <i class="el-icon-error" v-if="scope.row.cat_deleted == 'false'"></i>
         </tempalte>

          <!-- 树形表  排序列 -->
          <template slot="sort" slot-scope="scope" >
              <el-tag v-if="scope.row.cat_level === 0"  type="primary" size="small">一级</el-tag>
              <el-tag v-else-if="scope.row.cat_level === 1" type="success"  size="small">二级</el-tag>
              <el-tag v-else type="warning"  size="small">三级</el-tag>
          </template>
          <!-- 树形表  操作列 -->
            <template slot="opt" slot-scope="scope" >
                <el-button type="primary" size="mini" icon="el-icon-s-tools" @click="showEditCateDialog(scope.row)">编辑</el-button>
                <el-button type="danger" size="mini" icon="el-icon-delete-solid" @click="deleteCateById(scope.row)">删除</el-button>
          </template>
         </tree-table>

         <!-- 添加分类对话框 -->
         <el-dialog
            title="添加分类"
            :visible.sync="addCateDialogVisible"
            width="50%" @close = 'addCateDialogClose' >
            <!-- 表单 -->
            <el-form ref="addCateFormRef"   :model="addCateForm" label-width="80px">
                <el-form-item label="分类名称"  >
                    <el-input v-model="addCateForm.cat_name"  clearable></el-input>
                </el-form-item>
                <el-form-item label="父级分类"  >
                    <el-cascader
                        v-model="selectedKeys"
                        :options="parentCateList"
                        expand-trigger="hover"
                        :props="cascaderProps "
                        @change="parentCateChanged" clearable change-on-select=""></el-cascader>
                    <!-- <el-input v-model="addCateForm.cat_level" prop="cat_name" clearable></el-input> -->
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addCateDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addCate">确 定</el-button>
            </span>
            </el-dialog>

          <!-- 编辑按钮触发弹窗 -->
             <el-dialog
            title="编辑分类"
            :visible.sync="editCateDialogVisible"
            width="50%" @close = 'editCateDialogClose' >
            <!-- 表单 -->
            <el-form    :model="editCateForm" label-width="80px">
                <el-form-item label="分类名称"  >
                    <el-input v-model="editCateForm.cat_name"    clearable></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editCateDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editCate">确 定</el-button>
            </span>
            </el-dialog>
        <!-- 分页 -->
         <el-pagination
            :page-sizes="[1,  5, 10,20,50]"
            :page-size="10"
            :current-page.sync="queryinfo.pagenum"
             @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
     </el-pagination>
    </div>
</template>

<script>
export default {
    data(){
        return{
            // 商品分类
            cateList:[], 
            // 查询条件
            queryinfo:{
                type:3,
                pagenum:1,
                pagesize:10
            },
            // 添加分类对话框显示隐藏
            addCateDialogVisible: false,
            editCateDialogVisible: false,
            deleteCateDialogVisible: false,
            total:0,
            // 给tree-table指定列
            columns: [
            {
                label: '分类名称',
                prop: 'cat_name',
            },{
                 label:'是否有效',
                 type: 'template',
                 template: 'isok',

            },{
                label:'排序',
                type:'template',
                template:'sort'
            },{
                label:'操作',
                type:'template',
                template:'opt'
            }],
            // 添加分类的表单数据
            addCateForm:{
                cat_pid:0,
                cat_name:'',
                cat_level:0,
            },
           editCateForm:{
               cat_pid:0,
                cat_name:'',
                cat_level:0,
           },
           deleteCateForm:{
               cat_id:0,
               cat_pid:0,
                cat_name:'',
                cat_level:0,
           },
        
            parentCateList:[],
            cascaderProps:{
                value:'cat_id',
                label:'cat_name',
                children:'children'
            },
            selectedKeys:[],
        }
    },
    created(){
        this.getCateList();
    },
    methods:{
        // 获取商品分类数据
       async getCateList(){
            const {data:res} = await this.$http.get('categories',{
                params:this.queryinfo
            })
            if(res.meta.status !== 200 ) return this.$Message.error('分类列表数据请求失败！')
                this.total = res.data.total
                this.cateList = res.data.result
                
        },
        // 监听pagesize改变
        handleSizeChange(newSize){
                this.queryinfo.pagesize = newSize;
                this.getCateList();
        },
        // 监听pagenum改变
        handleCurrentChange(newPagenum){
            this.queryinfo.pagenum = newPagenum
             this.getCateList();
        },
        // 点击添加分类按钮
        showAddCateDialog(){
            this.getParentCateList()
            this.addCateDialogVisible = true;
        },
       
       async getParentCateList(){
            const {data : res}  = await this.$http.get('categories',{
                params:{
                    type:2
                },
            })
            if(res.meta.status !== 200) return this.$Message.error('数据获取失败！')
            this.parentCateList = res.data;
        },
        parentCateChanged(){
            const slKeys = this.selectedKeys;
            if(slKeys.length > 0){
                this.addCateForm.cat_pid  = slKeys[ slKeys.length-1];
                
                
                this.addCateForm.cat_level = slKeys.length;
                return
            }else{
                 this.addCateForm.cat_pid = 0;
                   this.addCateForm.cat_level = 0;
            };
        },
        // 关闭对话框重置表当数据
        addCateDialogClose(){
            this.addCateForm = {};
            this.selectedKeys = [];
             this.addCateForm.cat_pid = 0;
              this.addCateForm.cat_level = 0;
        },
         // 添加分类提交
      async  addCate(){
         const {data:res}  = await  this.$http.post('categories',this.addCateForm)
         if(res.meta.status != 201 ) return this.$Message.error('分类添加失败')
             this.addCateDialogVisible = false;
            this.$Message.success('分类添加成功')
            this.getCateList();
        },
        // 编辑分类名称
        editCateDialogClose(){
            this.editCateForm = {}
        },
        showEditCateDialog(cate){
            this.editCateForm = cate
            
            this.editCateDialogVisible = true;
            
        },
      
        async editCate(){

            const {data : res} = await this.$http.put(`categories/${this.editCateForm.cat_id}`,{
                cat_name:this.editCateForm.cat_name
            })
            if(res.meta.status !== 200) return this.$Message.error('分类名称更新失败')
              this.editCateDialogVisible = false;
             this.getCateList();
            this.$Message.success('分类名称更新成功')
        },
        // 删除分类
       deleteCateById(cateinfo){
             this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then( async () => {
                this.deleteCateForm = cateinfo;
                const {data:res} =  await this.$http.delete(`categories/${ this.deleteCateForm.cat_id}`)
                if(res.meta.status !==200) return this.$message.error('分类删除失败')
                this.$message.success('分类删除成功');
                  this.getCateList();
                }).catch(() => {
                    this.$message({
                    type: 'info',
                    message: '已取消分类删除操作'
                });          
            });
       }
    }
}
</script>

<style lang="less" scoped>
.el-breadcrumb{
    margin-bottom:15px;
}
.el-icon-success{
    color:lightgreen;
    font-size:15px;
}
.el-icon-error{
    color:red;
      font-size:15px;
}
.tree-table,.el-pagination{
    margin-top: 10px;
}
</style>